package com.cms.biz.rsf;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.cms.bean.RSFBean;

public class RSFHelper {
	
	public static void AddRSF(Connection conn,RSFBean rsfBean){		
		PreparedStatement pst = null;		
		try {
			pst = conn.prepareStatement(SQLString.ADD_RSF);
			pst.setInt(1, rsfBean.getId());
			pst.setInt(2, rsfBean.getFolderId());
			pst.setInt(3, rsfBean.getCategoryId());
			pst.setInt(4, rsfBean.getSpecialId());
			pst.setString(5, rsfBean.getTitle());
			pst.setString(6, rsfBean.getTitleColor());
			pst.setString(7, rsfBean.getAuthor());
			pst.setString(8, rsfBean.getSource());
			pst.setInt(9, rsfBean.getCount());
			pst.setInt(10, rsfBean.getIsTop());
			pst.setInt(11, rsfBean.getIsImg());
			pst.setString(12, rsfBean.getImgName());
			pst.setInt(13, rsfBean.getIsPassd());
			pst.setString(14, rsfBean.getKeyWord());
			pst.setInt(15, rsfBean.getSubCategoryId());
			pst.setTimestamp(16, rsfBean.getcDate());
			pst.setString(17, rsfBean.getcUser());
			pst.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {

			if (pst != null) {
				try {
					pst.close();
					pst = null;
				} catch (final SQLException e) {
				}
			}
		}		
	}

	
	public static void updateRSF(Connection conn,RSFBean rsfBean){
		PreparedStatement pst = null;		
		try {
			
			pst = conn.prepareStatement(SQLString.UPDATE_RSF);
			pst.setInt(1, rsfBean.getFolderId());
			pst.setInt(2, rsfBean.getSubCategoryId());
			pst.setInt(3, rsfBean.getSpecialId());
			pst.setString(4, rsfBean.getTitle());
			pst.setString(5, rsfBean.getTitleColor());
			pst.setString(6, rsfBean.getAuthor());
			pst.setString(7, rsfBean.getSource());
			pst.setInt(8, rsfBean.getIsTop());
			pst.setInt(9, rsfBean.getIsImg());
			pst.setString(10, rsfBean.getImgName());
			pst.setInt(11, rsfBean.getIsPassd());
			pst.setString(12, rsfBean.getKeyWord());
			pst.setInt(13, rsfBean.getId());
			pst.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {

			if (pst != null) {
				try {
					pst.close();
					pst = null;
				} catch (final SQLException e) {
				}
			}
		}
	}
	
	@SuppressWarnings("finally")
	public static RSFBean getRsfBeanById(Connection conn, int rsf_id) {
		PreparedStatement pst = null;
		ResultSet rSet = null;
		RSFBean rsfBean = null;
		try {
			pst = conn.prepareStatement(SQLString.GET_RSF_BY_ID);
			pst.setInt(1, rsf_id);
			rSet = pst.executeQuery();
			while (rSet.next()) {
				rsfBean = new RSFBean();
				rsfBean.setId(rSet.getInt("id"));
				rsfBean.setFolderId(rSet.getInt("folder_id"));
				rsfBean.setCategoryId(rSet.getInt("category_id"));
				rsfBean.setSubCategoryId(rSet.getInt("sub_category_id"));
				rsfBean.setSpecialId(rSet.getInt("special_id"));
				rsfBean.setTitle(rSet.getString("title"));
				rsfBean.setTitleColor(rSet.getString("title_color"));
				rsfBean.setAuthor(rSet.getString("author"));
				rsfBean.setSource(rSet.getString("source"));
				rsfBean.setCount(rSet.getInt("count"));
				rsfBean.setIsTop(rSet.getInt("istop"));
				rsfBean.setIsImg(rSet.getInt("isimg"));
				rsfBean.setImgName(rSet.getString("img_name"));
				rsfBean.setIsPassd(rSet.getInt("ispassd"));
				rsfBean.setKeyWord(rSet.getString("keywords"));
				rsfBean.setcDate(rSet.getTimestamp("create_date"));
				rsfBean.setcUser(rSet.getString("create_user"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (rSet != null) {
				try {
					rSet.close();
					rSet = null;
				} catch (final SQLException e) {
				}
			}
			if (pst != null) {
				try {
					pst.close();
					pst = null;
				} catch (final SQLException e) {
				}
			}
			return rsfBean;
		}
	}
}
